package drone.iqgc.util;

import org.locationtech.proj4j.CRSFactory ;
import org.locationtech.proj4j.CoordinateReferenceSystem ;
import org.locationtech.proj4j.CoordinateTransform ;
import org.locationtech.proj4j.CoordinateTransformFactory ;
import org.locationtech.proj4j.Proj4jException ;
import org.locationtech.proj4j.ProjCoordinate ;

public class Wgs84Util
{
	
	private static final CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
    private static final CRSFactory crsFactory = new CRSFactory();
	 /**
     * 高斯投影坐标转WGS84经纬度
     * @param x 高斯东坐标（单位：米）
     * @param y 高斯北坐标（单位：米）
     * @param centralMeridian 中央子午线经度（单位：度）
     */
    public static double[] convert(double x, double y, double centralMeridian) throws Proj4jException {
        String sourceCrsParams = String.format(
            "+proj=tmerc +lat_0=0 +lon_0=%.6f +k=1 +x_0=500000 +ellps=WGS84", 
            centralMeridian
        );
        
        CoordinateReferenceSystem srcCrs = crsFactory.createFromParameters("GAUSS", sourceCrsParams);
        CoordinateReferenceSystem dstCrs = crsFactory.createFromName("EPSG:4326"); // WGS84
        
        CoordinateTransform transform = ctFactory.createTransform(srcCrs, dstCrs);
        ProjCoordinate result = new ProjCoordinate();
        transform.transform(new ProjCoordinate(x, y), result);
        
        return new double[]{result.y, result.x}; // [纬度, 经度]
    }
    
    public static void main(String[] args) {
        try {
            // 示例：济南某点高斯坐标（假设中央子午线117°）
            double x = 385000.0; // 东坐标（米）
            double y = 4034567.0; // 北坐标（米）
            double centralMeridian = 117.0; // 中央子午线经度
            
            double[] wgs84 = convert(x, y, centralMeridian);
            System.out.printf("WGS84坐标: 纬度=%.6f°, 经度=%.6f°", wgs84[0], wgs84[1]);
        } catch (Proj4jException e) {
            e.printStackTrace();
        }
    }
}
